package com.chw.shop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chw.common.exception.ParamExcption;
import com.chw.shop.dao.ShopDao;
import com.chw.shop.dao.ShopimgService;
import com.chw.shop.entity.po.Shop;
import com.chw.shop.entity.po.ShopImg;
import com.chw.shop.entity.po.ShopSpecs;
import com.chw.shop.entity.vo.ShopImgVo;
import com.chw.shop.service.ISpecsService;
import com.chw.shop.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ShopServiceimpl implements ShopService {
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private ShopimgService imgService;
    @Autowired
    private ISpecsService iSpecsService;
    @Override
    public Object queryShop(Integer id,Integer page,Integer size,Integer brandId) {
        QueryWrapper<Shop> wrapper=new QueryWrapper();
        if (page==null || size==null){
            throw  new ParamExcption();
        }
        if (id!=null){
            wrapper.eq("typeId",id);
        }
        if (brandId!=null){
            wrapper.eq("brandId",brandId);
        }

        IPage<Shop> p = new Page<>(page,size);
        IPage iPage1 = shopDao.selectPage(p, wrapper);
        List<Shop> records = iPage1.getRecords();
        long current = iPage1.getTotal();
        System.out.println(iPage1.getTotal());
        Map map=new HashMap();
        map.put("data",records);
        map.put("count",current);
        return map;
    }

    @Override
    public Object selectShop(Integer id) {
        if (id==null){
            throw  new ParamExcption();
        }

       ShopImgVo shopImgVos = (ShopImgVo) shopDao.queryShop(id);
        List<String>imgs = imgService.queryImgs(id);
        imgs.add(0,shopImgVos.getImg());
        shopImgVos.setImgs(imgs);
       List<ShopSpecs> shopSpecs = (List<ShopSpecs>) iSpecsService.queryShopSpecs(id);
       int count=0;
       List<List> shopSpecs1=new ArrayList<>();
       List one=new ArrayList();
        for (ShopSpecs s:shopSpecs) {
            ShopSpecs specs=s;
            JSONObject parse = (JSONObject) JSONObject.parse(specs.getValuess());
            if (parse.getDouble("price")!=null){
                count++;
                Map map=new HashMap();
                map.put("sid",specs.getId());
                map.put("price",parse.getDoubleValue("price"));
                StringBuffer stringBuffer=new StringBuffer();
                Set<String> set=parse.keySet();
                for (String k:set) {
                    if (k.equals("count") || k.equals("price")){
                        continue;
                    }
                    stringBuffer.append(iSpecsService.getName(k,parse.getString(k)).get(0)).append("-");
                }
                map.put("intro",stringBuffer.substring(0,stringBuffer.length()-1));
                one.add(map);
                if (count%3==0){
                    shopSpecs1.add(one);
                    one=new ArrayList();
                }
            }
        }
        if (count%3!=0){
            shopSpecs1.add(one);

        }
        shopImgVos.setShopSpecs(shopSpecs1);
        return shopImgVos;
    }
}
